# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2024/1/18 14:06
from telnetlib import EC

from selenium.common.exceptions import NoSuchElementException

from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.user_product_computing_locator import ProductComputingLocator
from Case.python_ui.PageLocate.user_product_mangodb_locator import ProductMangodbReplicaLocator
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException


import time


class ProductMangodbReplica(Basepage):

    # 根据tab菜单名称进行点击
    def click_my_resource_tab(self, tab_name):
        doc = '根据my resource页的tab菜单名字进行点击进入'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.my_resource_tab_list, doc)
        my_resource_tab_list = self.get_elements(ProductComputingLocator.my_resource_tab_list, doc)
        for i in my_resource_tab_list:
            print('*****:', i.text, type(i))
            if i.text == tab_name:
                print('++++:', i.text)
                i.click()
        time.sleep(2)

    # 根据产品名称进行点击
    def get_product_name_to_click(self, product_name):
        doc = '根据产品名称然后点击'
        prod_list = self.get_elements(ProductComputingLocator.my_resource_product_list, doc)
        time.sleep(2)
        for prod in prod_list:
            print('+++++:', prod.text)
            if prod.text == product_name:
                prod.click()
                break
        time.sleep(5)

    # tab页根据产品名称进行点击进入控制台
    def click_product_by_name(self, product_name):
        doc = 'tab页根据产品名称进行点击进入控制台'
        time.sleep(5)
        self.wait_elevisble(ProductComputingLocator.move_computing_create_new, doc)
        self.move_mouse_to_element(ProductComputingLocator.move_computing_create_new)
        time.sleep(2)
        self.get_product_name_to_click(product_name)
        time.sleep(2)


    # 进入mangodb replica管理控制页
    def enter_mangodb_replica_page(self):
        doc = 'mangodb replica管理控制页'
        self.click_my_resource_tab('Database')
        self.click_product_by_name('MongoDB Replica Set')
        self.switch_to_new_window()
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_enter_mangodb_replica_page, doc)
        print("iframe++++:",self.get_element(ProductMangodbReplicaLocator.if_enter_mangodb_replica_page, doc).text)
        #print("+++:", self.get_element(ProductBlockStorgeLocator.if_enter_block_storage_page, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_enter_mangodb_replica_page, doc).text == 'MongoDB Replica Set'
        time.sleep(5)

    # 进入mangodb sharded管理控制页
    def enter_mangodb_sharded_page(self):
        doc = 'mangodb sharded管理控制页'
        self.click_my_resource_tab('Database')
        self.click_product_by_name('MongoDB Sharded Cluster')
        self.switch_to_new_window()
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_enter_mangodb_sharded_page, doc)
        print("iframe++++:",self.get_element(ProductMangodbReplicaLocator.if_enter_mangodb_sharded_page, doc).text)
        #print("+++:", self.get_element(ProductBlockStorgeLocator.if_enter_block_storage_page, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_enter_mangodb_replica_page, doc).text == 'MongoDB Sharded Cluster'
        time.sleep(5)

     #副本集订购
    def create_mangodb_replica(self):
        doc = '副本集订购'
        #self.scroll_bar_operation(ProductMangodbReplicaLocator.click_terms_of_service)

        self.wait_elevisble(ProductMangodbReplicaLocator.click_buy_now, doc)
        print('buy now++:', self.get_element(ProductMangodbReplicaLocator.click_buy_now, doc).text)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_buy_now, doc)
        time.sleep(10)
        #self.wait_elevisble(ProductMangodbReplicaLocator.click_terms_of_service, doc)
        print("terms++++:", self.get_element(ProductMangodbReplicaLocator.click_terms_of_service, doc).text)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_terms_of_service, doc)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_pay, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_pay, doc)
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_payment_succ, doc)
        print("payment++++:", self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text == 'Thank you for your payment. You can view the order details in order management or open the console for related operations.'
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_user_center, doc)
        time.sleep(10)

     #检查副本集实例状态
    def check_instance_status(self):
        doc = '检查实例状态'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_instance_id, doc)
        for i in range(3):
            try:
                self.wait_elevisble(ProductMangodbReplicaLocator.iframe_instance_status, doc)
                self.driver.switch_to.frame(self.get_element(ProductMangodbReplicaLocator.iframe_instance_status, doc))
            except:
                continue
        time.sleep(5)
        while True:
            try:
                print('creating+:', self.get_element(ProductMangodbReplicaLocator.click_status_creating, doc).text)
            except NoSuchElementException:
                break
            else:
                self.wait_elevisble(ProductMangodbReplicaLocator.click_search, doc)
                self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_search, doc)
                time.sleep(2)
                try:
                    print('creating++:', self.get_element(ProductMangodbReplicaLocator.click_status_run, doc).text)
                    break
                except NoSuchElementException:
                    pass
        self.wait_elevisble(ProductMangodbReplicaLocator.click_status_run, doc)
        print('run++:',self.get_element(ProductMangodbReplicaLocator.click_status_run, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.click_status_run, doc).text == 'Running'
        time.sleep(5)
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[0])
        time.sleep(5)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_search_mango, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_search_mango, doc)
        time.sleep(5)

    # 回到用户中心
    def back_to_usercenter(self):
        doc = '回到用户中心'
        self.wait_elevisble(ProductMangodbReplicaLocator.click_user_center, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_user_center, doc)
        time.sleep(5)

    #副本集变配
    def update_mangodb_replica(self):
        doc = '副本集变配'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_menu, doc)
        self.move_mouse_to_element(ProductMangodbReplicaLocator.click_menu)
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_modify, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_modify, doc)
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        time.sleep(10)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_add_capacity, doc)
        print("ADD++++:", self.get_element(ProductMangodbReplicaLocator.click_add_storage, doc).text)
        self.click_element(ProductMangodbReplicaLocator.click_add_storage, doc)
        time.sleep(5)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_renew_buy_now, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_renew_buy_now, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_terms_of_service, doc)
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_pay, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_pay, doc)
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_payment_succ, doc)
        print("payment++++:", self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text == 'Thank you for your payment. You can view the order details in order management or open the console for related operations.'
        time.sleep(2)

    #副本集release
    def release_mangodb_replica(self):
        doc = '副本集release'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_menu, doc)
        self.move_mouse_to_element(ProductMangodbReplicaLocator.click_menu)
        time.sleep(5)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_release, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_release, doc)
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        time.sleep(20)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_add_capacity, doc)
        #print("ADD++++:", self.get_element(ProductBlockStorgeLocator.click_release_again, doc).text)
        self.click_element(ProductMangodbReplicaLocator.click_release_again, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_yes_release, doc)
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_release_success, doc)
        print("release_success++++:", self.get_element(ProductMangodbReplicaLocator.if_release_success, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_release_success, doc).text == 'The resource has been released successfully.'
        time.sleep(2)

    #renew之后副本集release
    def renew_release_mangodb_replica(self):
        doc = '副本集release'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_menu, doc)
        self.move_mouse_to_element(ProductMangodbReplicaLocator.click_menu)
        time.sleep(5)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_release, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_release, doc)
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[3])
        time.sleep(20)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_add_capacity, doc)
        #print("ADD++++:", self.get_element(ProductBlockStorgeLocator.click_release_again, doc).text)
        self.click_element(ProductMangodbReplicaLocator.click_release_again, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_yes_release, doc)
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_release_success, doc)
        print("release_success++++:", self.get_element(ProductMangodbReplicaLocator.if_release_success, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_release_success, doc).text == 'The resource has been released successfully.'
        time.sleep(2)


    #副本集续订
    def renew_mangodb_replica(self):
        doc = '副本集续订'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_renew, doc)
        print("renew++++:", self.get_element(ProductMangodbReplicaLocator.click_renew, doc).text)
        time.sleep(5)
        all_handles = self.driver.window_handles
        self.driver.switch_to.window(all_handles[2])
        time.sleep(20)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_renew_buy_now, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_renew_terms_of_services, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_pay, doc)
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_payment_succ, doc)
        print("payment++++:", self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text == 'Thank you for your payment. You can view the order details in order management or open the console for related operations.'
        time.sleep(2)


     #分片集订购
    def create_mangodb_sharded(self):
        doc = '分片集订购'
        #self.scroll_bar_operation(ProductMangodbReplicaLocator.click_terms_of_service)

        self.wait_elevisble(ProductMangodbReplicaLocator.click_buy_now, doc)
        print('buy now++:', self.get_element(ProductMangodbReplicaLocator.click_buy_now, doc).text)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_buy_now, doc)
        time.sleep(10)
        #self.wait_elevisble(ProductMangodbReplicaLocator.click_terms_of_service, doc)
        print("terms++++:", self.get_element(ProductMangodbReplicaLocator.click_terms_of_service, doc).text)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_terms_of_service, doc)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_pay, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_pay, doc)
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_payment_succ, doc)
        print("payment++++:", self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text == 'Thank you for your payment. You can view the order details in order management or open the console for related operations.'
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_user_center, doc)
        time.sleep(10)

        # 检查分片集实例状态
    def check_sharded_instance_status(self):
        doc = '检查分片集实例状态'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_instance_id, doc)
        for i in range(3):
            try:
                self.wait_elevisble(ProductMangodbReplicaLocator.iframe_instance_status, doc)
                self.driver.switch_to.frame(self.get_element(ProductMangodbReplicaLocator.iframe_instance_status, doc))
            except:
                continue
        time.sleep(2)
        #self.wait_elevisble(ProductMangodbReplicaLocator.select_sharded, doc)
        print('shardinstance:',self.get_element(ProductMangodbReplicaLocator.select_sharded, doc).text)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.select_sharded, doc)
        time.sleep(5)
        while True:
            try:
                print('creating+:', self.get_element(ProductMangodbReplicaLocator.click_status_creating, doc).text)
            except NoSuchElementException:
                break
            else:
                self.wait_elevisble(ProductMangodbReplicaLocator.click_sharded_search, doc)
                self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_sharded_search, doc)
                time.sleep(2)
                try:
                    print('creating++:', self.get_element(ProductMangodbReplicaLocator.click_status_run, doc).text)
                    break
                except NoSuchElementException:
                    pass
        self.wait_elevisble(ProductMangodbReplicaLocator.click_status_run, doc)
        print('run++:', self.get_element(ProductMangodbReplicaLocator.click_status_run, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.click_status_run, doc).text == 'Running'

    #分片集变配
    def update_mangodb_sharded(self):
        doc = '分片集变配'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_menu, doc)
        self.move_mouse_to_element(ProductMangodbReplicaLocator.click_menu)
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_modify, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_modify, doc)
        self.switch_to_new_window()
        time.sleep(10)
        for i in range(3):
            try:
                self.wait_elevisble(ProductMangodbReplicaLocator.iframe_instance_status, doc)
                self.driver.switch_to.frame(self.get_element(ProductMangodbReplicaLocator.iframe_instance_status, doc))
            except:
                continue
        time.sleep(2)
        #self.wait_elevisble(ProductMangodbReplicaLocator.if_sharded_modify_page, doc)
        print("BASIC++++:", self.get_element(ProductMangodbReplicaLocator.if_sharded_modify_page, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_sharded_modify_page, doc).text == 'Basic Information'
        time.sleep(5)


    #分片集release
    def release_mangodb_sharded(self):
        doc = '分片集release'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_menu, doc)
        self.move_mouse_to_element(ProductMangodbReplicaLocator.click_menu)
        time.sleep(5)
        self.wait_elevisble(ProductMangodbReplicaLocator.click_release, doc)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_release, doc)
        self.switch_to_new_window()
        time.sleep(20)
        #self.wait_elevisble(ProductBlockStorgeLocator.click_add_capacity, doc)
        #print("ADD++++:", self.get_element(ProductBlockStorgeLocator.click_release_again, doc).text)
        self.click_element(ProductMangodbReplicaLocator.click_release_again, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_yes_release, doc)
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_release_success, doc)
        print("release_success++++:", self.get_element(ProductMangodbReplicaLocator.if_release_success, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_release_success, doc).text == 'The resource has been released successfully.'
        time.sleep(2)


    #分片集续订
    def renew_mangodb_sharded(self):
        doc = '分片集续订'
        self.click_my_resource_tab('Database')
        time.sleep(10)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_renew, doc)
        print("renew++++:", self.get_element(ProductMangodbReplicaLocator.click_renew, doc).text)
        time.sleep(5)
        self.switch_to_new_window()
        time.sleep(20)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_renew_buy_now, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_renew_terms_of_services, doc)
        time.sleep(5)
        self.move_mouse_to_click_element(ProductMangodbReplicaLocator.click_pay, doc)
        time.sleep(2)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_payment_succ, doc)
        print("payment++++:", self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_payment_succ, doc).text == 'Thank you for your payment. You can view the order details in order management or open the console for related operations.'
        time.sleep(2)



    # 进入RDS for PostgreSQL管理控制页
    def enter_postgresql_page(self):
        doc = 'PostgreSQL管理控制页'
        self.click_my_resource_tab('Database')
        self.click_product_by_name('RDS for PostgreSQL')
        self.switch_to_new_window()
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_enter_postgresql_page, doc)
        print("iframe++++:",self.get_element(ProductMangodbReplicaLocator.if_enter_postgresql_page, doc).text)
        #print("+++:", self.get_element(ProductBlockStorgeLocator.if_enter_block_storage_page, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_enter_postgresql_page, doc).text == 'RDS for PostgreSQL'
        time.sleep(5)

    # 进入sqlserver管理控制页
    def enter_sqlserver_page(self):
        doc = 'SQLserver管理控制页'
        self.click_my_resource_tab('Database')
        self.click_product_by_name('RDS for SQL Server')
        self.switch_to_new_window()
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_enter_sqlserver_page, doc)
        print("iframe++++:",self.get_element(ProductMangodbReplicaLocator.if_enter_sqlserver_page, doc).text)
        #print("+++:", self.get_element(ProductBlockStorgeLocator.if_enter_block_storage_page, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_enter_sqlserver_page, doc).text == 'RDS for SQL Server'
        time.sleep(5)

    # 进入mysql管理控制页
    def enter_mysql_page(self):
        doc = 'mysql管理控制页'
        self.click_my_resource_tab('Database')
        self.click_product_by_name('RDS for MySQL')
        self.switch_to_new_window()
        time.sleep(10)
        self.wait_elevisble(ProductMangodbReplicaLocator.if_enter_mysql_page, doc)
        print("iframe++++:",self.get_element(ProductMangodbReplicaLocator.if_enter_mysql_page, doc).text)
        #print("+++:", self.get_element(ProductBlockStorgeLocator.if_enter_block_storage_page, doc).text)
        assert self.get_element(ProductMangodbReplicaLocator.if_enter_mysql_page, doc).text == 'RDS for MySQL'
        time.sleep(5)






